package com.sloera.java.common.cloud.feign;

import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author liuwangyang
 * @version 1.0
 * @class com.sloera.java.common.cloud.feign.FeignRequestHeaderInterceptor
 * @date 2020-8-5
 */
public class FeignRequestHeaderInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null != attrs) {
            HttpServletRequest request = attrs.getRequest();
            String authorization = request.getHeader("Authorization");
            if (StrUtil.isNotBlank(authorization)) {
                template.header("Authorization", authorization);
            }
        }

    }
}
